package com.vitabuybuy.tv.ui.mine.talent.live

import android.os.Bundle
import android.view.View
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.FragmentLiveManageBinding
import com.library.common.base.BaseFragment
import com.library.common.base.MessageModel
import com.vitabuybuy.tv.dto.PlayBackLiveListDto
import com.vitabuybuy.tv.ui.mine.talent.live.viewmodel.LiveManageViewModel
import kotlinx.android.synthetic.main.fragment_live_manage.*
import kotlinx.android.synthetic.main.widget_title.*

class LiveManageFragment : BaseFragment<LiveManageViewModel, FragmentLiveManageBinding>() {
    override val layoutId: Int
        get() = R.layout.fragment_live_manage

    private var pageNumber = 1

    override fun initView(savedInstanceState: Bundle?) {
        mBinding.mViewModel = viewModel
    }

    override fun onResume() {
        super.onResume()
        viewModel.getPlayBackList(true, pageNumber)
    }

    override fun eventHandling() {
        tvManage.setOnClickListener {
            LiveReplayManageActivity.openActivity(mContext)
        }
        refresh.setOnRefreshListener(object : RefreshListenerAdapter() {
            override fun onLoadMore(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber++
                viewModel.getPlayBackList(true, pageNumber)
            }

            override fun onRefresh(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber = 1
                viewModel.getPlayBackList(true, pageNumber)
            }
        })
    }

    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            0 -> {
                pageNumber = 1
                viewModel.getPlayBackList(true, pageNumber)
            }
            1 -> {
                val item = msg.obj as PlayBackLiveListDto
                LiveStatisticsActivity.openActivity(mContext,item.id)
            }
            2 -> {
                val item = msg.obj as PlayBackLiveListDto
                LiveReplayActivity.openActivity(mContext,item.id)
            }
            3 -> {
                refresh.onLoad(msg.arg1)
            }
        }
    }
}
